package com.yunjijia.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunjijia.mapper.HouseMapper;
import com.yunjijia.pojo.House;
import com.yunjijia.pojo.HouseExample;
import com.yunjijia.service.HouseService;
import com.yunjijia.utils.PageResult;
import jdk.nashorn.internal.runtime.JSONFunctions;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Transactional
@Repository
public class HouseServiceImpl implements HouseService {


    @Resource
    private HouseMapper houseMapper;


    @Override
    public House selectByid(long id) {
        return houseMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(House house) {


        //放入第一张图片
        String images = house.getImages();
        List<Map> maps = JSON.parseArray(images, Map.class);
        if(null != maps && maps.size() > 0)
        {
             house.setImage((String) maps.get(0).get("url"));
        }

        houseMapper.insertSelective(house);
    }

    /**
     * 删除 是将状态改为下架
     *
     * @param id
     */
    @Override
    public void del(long id) {
        House house = houseMapper.selectByPrimaryKey(id);
        house.setStatus(1);
        houseMapper.updateByPrimaryKeySelective(house);
    }

    @Override
    public void update(House house) {
        //放入第一张图片
        String images = house.getImages();
        List<Map> maps = JSON.parseArray(images, Map.class);
        if(null != maps && maps.size() > 0)
        {
            house.setImage((String) maps.get(0).get("url"));
        }
        houseMapper.updateByPrimaryKeySelective(house);
    }

    @Override
    public List<House> selecrAll() {
        return houseMapper.selectByExample(null);
    }

    @Override
    public PageResult search(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<House> list = houseMapper.selectByExample(null);
        PageInfo pageInfo = new PageInfo(list);
        System.out.println(pageInfo.getList().size());
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public PageResult requirementSearch(int pageNum, int pageSize, House house) {

        PageHelper.startPage(pageNum, pageSize);
        HouseExample houseExample = new HouseExample();
        HouseExample.Criteria criteria = houseExample.createCriteria();

        //城市名称
        if (null != house.getCityname() && !"".equals(house.getCityname().trim())) {
            criteria.andCitynameEqualTo(house.getCityname().trim());
        }
        // 公司
        if (null != house.getName() && !"".equals(house.getName().trim())) {
            criteria.andNameEqualTo(house.getName().trim());
        }
        //小区名称
        if (null != house.getCommunityname() && !"".equals(house.getCommunityname().trim())) {
            criteria.andCommunitynameEqualTo(house.getCommunityname().trim());
        }
        //面积
        if (null != house.getArea()) {
            criteria.andAreaEqualTo(house.getArea());
        }
        //厅室
        if (null != house.getBeds()) {
            criteria.andBedsEqualTo(house.getBeds());
        }
        List<House> list = houseMapper.selectByExample(houseExample);
        PageInfo<House> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public List<Map> selectHouseCityList() {
        return houseMapper.selectHouseCityList();
    }

    @Override
    public List<Map> selectHouseBuiltList() {
        return houseMapper.selectHouseBuiltList();
    }

    @Override
    public List<Map> selectHouseCommunitynameList() {
        return houseMapper.selectHouseCommunitynameList();
    }

    @Override
    public List<Map> selectHousebedsList() {

        return houseMapper.selectHousebedsList();
    }

    @Override
    public List<Map> selectHouseAreaList() {
        return houseMapper.selectHouseAreaList();
    }
}
